<?php session_start();
$host  = $_SERVER['HTTP_HOST'];
if ($_SERVER['SERVER_NAME'] == 'localhost'){$uri="/kalender";}else{$uri="";}
if (!isset($_SESSION['id_user']) || $_SESSION['id_user']=='') {
	echo "<script language=Javascript> location.href='http://$host$uri/index.php'; </script>";	
}else{
	
	$id_user = $_SESSION['id_user'];	
	
	
	//funcion que asigna un nombre
	function get_rand_id($length){
		$codelenght = $length;
		$newcode_length = 0;
		$newcode = "";
		while($newcode_length < $codelenght) {
			$part = rand(1,3);
			if($part==1){$a=48;$b=57;}  // Numbers
			if($part==2){$a=65;$b=90;}  // UpperCase
			if($part==3){$a=97;$b=122;} // LowerCase
			$code_part=chr(rand($a,$b));
			$newcode_length = $newcode_length + 1;
			$newcode = $newcode.$code_part;
		}	
		return $newcode;
	}
	
	/* backup the db OR just a table */
	function backup_tables($fileName, $completeFilePath){
		$host='localhost';
		if ($_SERVER['SERVER_NAME'] == 'localhost'){
			$user='root';	
			$password='Tattoos';
			$name='kalender';
		}else{
			$host = "192.168.0.193";
			$user='kalender';
			$password='golfito82';
			$name='kalenderbeta';
		}
		
		$link = mysql_connect($host,$user,$password);
		mysql_select_db($name,$link);
		
		//get all of the tables		
		$tables = array();
		$result = mysql_query('SHOW TABLES');
		while($row = mysql_fetch_row($result)){
			$tables[] = $row[0];
		}
		
		
		//cycle through
		foreach($tables as $table){
			$result = mysql_query('SELECT * FROM '.$table);
			$num_fields = mysql_num_fields($result);
			
			$return.= 'DROP TABLE '.$table.';';
			$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
			$return.= "\n\n".$row2[1].";\n\n";
			
			for ($i = 0; $i < $num_fields; $i++) 
			{
				while($row = mysql_fetch_row($result))
				{
					$return.= 'INSERT INTO '.$table.' VALUES(';
					for($j=0; $j<$num_fields; $j++) 
					{
						$row[$j] = addslashes($row[$j]);
						$row[$j] = preg_replace("/\n/","/\\n/",$row[$j]);
						if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
						if ($j<($num_fields-1)) { $return.= ','; }
					}
					$return.= ");\n";
				}
			}
			$return.="\n\n\n";
		}
		
		//save file
		$handle = fopen($completeFilePath,'w+');
		fwrite($handle,$return);
		fclose($handle);
	}
	
	$ruta = $_SERVER["SCRIPT_FILENAME"];			
	if ($_SERVER['SERVER_NAME'] == 'localhost'){
		$ruta = explode("/",$ruta);
	}else{
		$ruta = explode("/",$ruta);
	}
	$aux = "";
	$separador = "";
	for ($j=0;$j < (sizeof($ruta)-2); $j++){
		$aux .= $separador . $ruta[$j];
		$separador = "/";
	}		
	$completeFilePath= $aux."/backups/";
	$fileName = get_rand_id(20).".bck";
	$completeFilePath = $completeFilePath.$fileName;
		
	backup_tables($fileName, $completeFilePath);
	
	echo("Se ha creado el backup de Usuarios y Archivos --> <a target='_blank' class='link f_bold' href='backups/".$fileName."'>".$fileName."</a> (Utilice 'Guardar enlace como...')");	
	
}
?>